package cn.lee.share.utils;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import cn.lee.TomcatConstants;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class FreemarkerUtils {
    private static final String templateFolder = TomcatConstants.WEB_ROOT + File.separator + "templates";
	public static void runTemplate(Map rootData, String template, Writer out) throws IOException, TemplateException{
		/* Create and adjust the configuration */ 
		Configuration cfg = new Configuration(); 
		cfg.setDirectoryForTemplateLoading(new File(templateFolder));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		/* Get or create a template */ 
		Template temp = cfg.getTemplate(template); 
		
		/* Merge data-model with template */ 
//		Writer out = new OutputStreamWriter(System.out); 
		temp.process(rootData, out); 
		out.flush(); 
		
	}
}
